const grant_type = 'client_credentials'
const client_id = 'sANfkHUTYUMy8RMK084AZxQS'
const client_secret = '9iI7hxsDWxm3FWgPIA4LTz3sEGqXisEj'
const app = getApp()
// pages/camera/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isCamera:false,
    src:'',
    list:[],
    token:'',
    aa:''
  },
  // camera(){
  //   wx.chooseImage({
  //     count: 1,
  //     sizeType: ['original', 'compressed'],
  //     sourceType: ['album', 'camera'],
  //     success (res) {
  //       // tempFilePath可以作为img标签的src属性显示图片
  //       const tempFilePaths = res.tempFilePaths
  //     }
  //   })
  // },

  camera_list(){
    wx.getSetting().then(res=>{
      if(res.authSetting['scope.camera']){
        this.setData({
          isCamera:true
        })
      }else{
        wx.authorize({
          scope: 'scope.camera', })
          .then(res=>{
            that.setData({
              isCamera:true
             })
        })
        .catch(err=>{
          this.get_camera_auth()
          
        })
       }
    })
  },
  get_camera_auth(){
    let that = this
    wx.showModal({
      title:'提示',
      content:'未授权无法进行拍照功能',
      confirmText:'授权',
      success(res){
        if(res.confirm) {
        wx.openSetting().then(res=>{
          console.log(res)
          if(res.authSetting['scope.camera']){
            that.setData({isCamera:true})
          }
        })
      }
      }
    })
  },
  //点击按钮首先获取token
  camera(){
    let that = this
    wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type='+grant_type+'&client_id='+client_id+'&client_secret='+client_secret,
      method:'POST',
      complete:function(res){
        console.log('执行success')
      },
      success:function(res){
        console.log('成功')
        console.log(res)
        that.setData({
          token:res.data.access_token
        })
        console.log('My token'+that.data.token)
        that.cam(res.data.access_token)
      },
      fail:function(err){
        console.log('错误')
        console.log(err)
      }
    })
  },
  //已经获取token，在请求图片
  cam(token){
    let that =this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        that.setData({
          isCamera:false,
          src:tempFilePaths
        })
        console.log(that.data.src)
        wx.getFileSystemManager().readFile({
          filePath:res.tempFilePaths[0],
          encoding:'base64',
          complete:res=>{
            console.log('complete')
          },
          success:res=>{
            console.log('11111s')
            that.setData({
              aa:res.data
            })
            wx.showLoading({
              title: '图片识别中',
            })
            wx.request({            //根据请求来的tokn值和图片的base64编码来请求百度api结果
              url: 'https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general?access_token=' + token,
              method:"POST",
              data:{
                image:res.data
              },
              header: {
                'Content-Type': 'application/x-www-form-urlencoded'
                },
              success:function(res){
                console.log(res.data)
                that.setData({
                  list:res.data.result
                })
                wx.hideLoading()
              },
              fail:function(err){
              }
            })
          }
        })
      }

    })
    
  },
  jumps(e){
    wx.navigateTo({
      url: '../../pages/search/index',
    })
    console.log(e.currentTarget.dataset.name)
    app.globalData.res =e.currentTarget.dataset.name
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.camera_list()
    this.ctx = wx.createCameraContext()
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})